<?php
/**
 * User: samuelqueniart
 * Date: 24/01/13
 * Time: 11:14
 */
require_once("MoneyFactory.class.php");
require_once("MoneyOps.class.php");
require_once ("MoneyExceptions.php");

$op = new operationAction($_GET['operation']);
$op->index($_GET['operation']);

class operationAction{

    public function index($ops){

        /* opération transmise*/
        switch($ops){
            case 'add':
                $this->add($_GET["v1"],$_GET["c1"],$_GET['PorteMonnaie'],$_GET['Devise']);
                break;
            default:
                throw new InvalidOperationException("invalid operation");
        }
    }

    private function add($value,$devise,$pm,$d){

        if (isset($value) && isset($devise)){
            $m1=MoneyFactory::getDefaultFactory()->createMoney($value,$devise);
            $m2=MoneyFactory::getDefaultFactory()->createMoney($pm,$d);
            $res=MoneyOps::add($m1,$m2);

            if (isset($res) && ($res instanceOf Money)) {
                echo '{"Etat": "ok", "Valeur" :"'.$res->getValue().'"}';
            }
            else{
                echo '{"Etat": "error", "Error" :"add fail"}';
            }
        }

    }
}
